#include "TestWrapper.h"

// implementation code of WrapperFactory - do NOT modify the next 5 lines
AbstractWrapper* WrapperFactory::wrapper = 0;
AbstractWrapper* WrapperFactory::createWrapper() {
  if (wrapper == 0) wrapper = new TestWrapper;
  return wrapper;
}
// Do not modify the following line
volatile bool TestWrapper::GlobalStop = false;

// a default constructor
TestWrapper::TestWrapper() {
  // create any objects here as instance variables of this class
	ast = new AST();
	var = &list;
  // as well as any initialization required for your spa program
}

// method for parsing the SIMPLE source
void TestWrapper::parse(std::string filename) {
	// call your parser to do the parsing
	Parser p = Parser(ast);
	p.Parse("test.txt");
	// ...rest of your code...
	DesignExtractor DesExt(ast);
	DesExt.fillTable();
}

// method to evaluating a query
void TestWrapper::evaluate(std::string query, std::list<std::string>& results){
// call your evaluator to evaluate the query here
  // ...code to evaluate query...
	std::vector<string> result;
	splitQuery(query, result);

	for(int i=0;i<result.size();i++)
	{   
		results.push_back(result.at(i));
	}
  // store the answers to the query in the results list (it is initially empty)
  // each result must be a string.
}

void TestWrapper::splitQuery(std::string query, std::vector<std::string>& results){
	int pos;
	std::string declPart, queryPart;
	pos=query.find("Select");

	if(pos!=-1)
	{
			declPart=query.substr(0,pos-1);
			queryPart=query.substr(pos, query.length()-1);
			SplitDeclaration SpDc; 
			QueryEvaluator obj;
			QRYSTATEMENT_TYPE asstType;
			asstType=SpDc.splitDeclaration(obj, declPart,var);
			int isValidQuery;
			
			if(asstType!=_INVALID)
			{
				isValidQuery = obj.validateQuery(queryPart);

				if(isValidQuery!=-1)
				{
					obj.executeQuery(results);
				}

			}
	}
}